﻿#pragma once
#include<string.h> 
#include<assert.h> 

//【命名空间】

namespace myspace
{
	class string
	{
	public:
		typedef char* iterator;
		iterator begin()
		{
			return _str;
		}
		iterator end()
		{
			return _str + _size;
		}

		string(const char* str = "")
		{
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_capacity+1];
			strcpy(_str, str);
		}
		/*拷贝构造*/string(const string& s)//现代深拷贝写法
			:_str(nullptr),_size(0),_capacity(0)
		{
			string tmp(s._str);
			swap(tmp);
		}
		void swap(string& s)
		{
			//using std::swap;
			::swap(_str, s._str);
			::swap(_size, s._size);
			::swap(_capacity, s._capacity);
		}

		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = _capacity = 0;
		}
		size_t size()const
		{
			return _size;
		}
		size_t capacity()const
		{
			return _capacity;
		}
		char& operator[](size_t i)
		{
			assert(i < _size);
			return _str[i];
		}
		const char& operator[](size_t i)const
		{
			assert(i < _size);
			return _str[i];
		}
		const char* c_str()
		{
			return _str;
		}
		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* newstr = new char[n + 1];
				strcpy(newstr, _str);
				delete[]_str;
				_str = newstr;
				_capacity = n;
			}
		}
		void push_back(char ch)
		{
			/*if (_size == _capacity)
			{
				size_t newcapacity = _capacity == 0 ? 2 : _capacity * 2;
				reserve(newcapacity);
			}
			_str[_size] = ch;
			++_size;
			_str[_size] = '\0';*/
			insert(_size, ch);
		}
		void append(const char* str)
		{
		/*	size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}
			strcpy(_str + _size, str);
			_size += len;*/
			insert(_size, str);
		}
		string& operator+=(char ch)
		{
			this->push_back(ch);
			return *this;
		}
		string& operator+=(const char* str)
		{
			this->append(str);
			return *this;
		}
		string& operator+=(string& s)
		{
			assert(this != &s);
			this->append(s._str);
			return *this;
		}
		/*作业：赋值*/string& operator=(string s)
		{
			if (&s != this)
			{
				swap(s); 
			}
			return *this;
		}
		void resize(size_t n, char ch='\0')
		{
			if (n < _size)
			{
				_str[n] = '\0';
				_size = n;
			}
			else
			{
				if (n > _capacity)
					reserve(n);
				for (size_t i = _size; i < n; ++i)
				{
					_str[i] = ch;
				}
				_size = n;
				_str[_size] = '\0';
			}
		}
		string& insert(size_t pos, char ch)
		{
			assert(pos <= _size);
			if (_size == _capacity)
			{
				size_t newcapacity = _capacity == 0 ? 2 : 2 * _capacity;
				reserve(newcapacity);
			}
			int end = _size;
			while (end >= (int)pos)
			{
				_str[end + 1] = _str[end];
				--end;
			}
			_str[pos] = ch;
			++_size;
			return *this;
		}
		string& insert(size_t pos, const char* str)
		{
			assert(pos <= _size);
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}
			int end = _size;
			while (end >= (int)pos)
			{
				_str[end + len] = _str[end];
				--end;
			}
			strncpy(_str + pos, str,len);
			_size += len;
			return *this;
		}

		string& erase(size_t pos, size_t len = npos)
		{
			assert(pos < _size);
			if (_size - pos <= len)
			{
				_size = pos;
				_str[pos] = '\0';
			}
			else
			{
				for (size_t i = 0; i < _size-len; ++i)
				{
					_str[pos + i] = _str[pos + i + len];
				}
				_size -= len;
			}
			
			return *this;
		}
		size_t find(char ch, size_t pos = 0)
		{
			assert(pos < _size);
			for (size_t i = pos; i < _size; ++i)
			{
				if (_str[i] == ch)return i;
			}
			return npos;
		}
		size_t find(const char* str, size_t pos = 0)
		{
			assert(pos < _size);
			char* p= strstr(_str, str);
			if (p == nullptr)
			{
				return npos;
			}
			else
			{
				return p - _str;
			}
		}
		bool operator<(const string& s)
		{
			int ret=strcmp(_str, s._str);
			return ret < 0;
		}
		bool operator==(const string& s)
		{
			int ret = strcmp(_str, s._str);
			return ret == 0;
		}
		bool operator>(const string& s)
		{
			return !(*this <= s);
		}
		bool operator<=(const string& s)
		{
			return *this < s || *this == s;
		}
		bool operator>=(const string& s)
		{
			return !(*this < s);
		}
		bool operator!=(const string& s)
		{
			return !(*this == s);
		}

	private:
		char* _str;
		size_t _size;
		size_t _capacity;

		static size_t npos;
	};
	size_t string::npos = static_cast<size_t>(-1);//显示类型转换
	ostream& operator<<(ostream& out, const string& s)
	{
		for (size_t i = 0; i < s.size(); ++i)
		{
			cout << s[i];
		}
		return out;
	}
	istream& operator>>(istream& in, string& s)
	{
		while (1)
		{
			char ch;
			//in >> ch;
			ch = in.get();
			if (ch == ' ' || ch == '\n')
			{
				break;
			}
			else
			{
				s += ch;
			}
		}
		return in;
	}

	void test_string1()
	{
		string s1("hello");
		cout << s1.size() << endl;
		cout << s1.c_str() << endl;
		s1.insert(1, 'h');
		cout << s1 << endl;
		cout << s1.size() << endl;
		cout << s1.c_str() << endl;
		s1.insert(1, "123456");
		cout << s1 << endl;
		cout << s1.size() << endl;
		cout << s1.c_str() << endl;
		s1.resize(20, 'a');
		cout << s1 << endl;
		cout << s1.size() << endl;
		cout << s1.c_str() << endl;
		s1.erase(2,5);
		cout << s1 << endl;
		cout << s1.size() << endl;
		cout << s1.c_str() << endl;
		s1.find('a',0);
		cout << s1.find('a', 0)<< endl;
	}


	void test_string2()
	{
		string s2;
		cin >> s2;
		cout << s2 << endl;
		string s3(s2);
		cout << s3 << endl;
	}
}



